let curPage = 0

// 获取分类
function getCatogary() {
    request({
        type: 'get',
        url: '/admin/category/list',
        success: (res) => {
            if(res.code === 200){
                const renderResult = template('cateList', res)
                $('#selCategory').html(renderResult)
            }
        }
    })
}
getCatogary()

// 获取文章列表
function getArticleList(page) {
    const type = $('#selCategory').val()
    const state = $('#selStatus').val()
    request({
        type: 'get',
        url: '/admin/article/query',
        data: { type, state,page },
        success: (res) => {
            if (res.code === 200) {
                const renderResult = template('articleList', res.data)
                $('tbody').html(renderResult)
                initPagination(res.data.totalPage)
            }
        }
    })
}
getArticleList()

function getArticleList1() {
    $('#pagination').empty();
    $('#pagination').removeData("twbs-pagination");
    $('#pagination').unbind('page');
    getArticleList()
}

// 删除某一条数据
function deleteArticle(id) {
    request({
        type: 'post',
        url: '/admin/article/delete',
        data: { id },
        success: (res) => {
            if (res.code === 204) {
                alert(res.msg)
                getArticleList(curPage)
            }
        }
    })
}

function initPagination(total) {
    $("#pagination").twbsPagination({
        totalPages: total,
        visiblePages:5,
        startPage:1,
        first:"首页",
        last:"页尾",
        prev:"上一页",
        next:"下一页",
        onPageClick: function (e, page) {
            curPage = page
            getArticleList(page)
        }
    })
}
